﻿package {

	import Sprites;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;

	public class Boy extends Sprites {
		
		public var myTween:Tween;
		private var walkingSound:Sound;
		private var freeCounter_1:int;
		public var caughtTheThief:Boolean;
		public var thiefIndex:int;
		
		//private var p1:Purchase;
		
		public function Boy(size:Number,left:Number,right:Number,up:Number,down:Number, target:Array) {
			super(size,left,right,up,down);
			this.initial_sprite_dir = "back";
			animals=target;
			//p1 = new Purchase();
			loadSounds();
			caughtTheThief = false;
			addListeners();
		}
		
		public function loadSounds():void {
			walkingSound = new Sound(new URLRequest("Audio/walking.mp3"));
		}
		
		public function addListeners():void {
			this.addEventListener(Event.ENTER_FRAME, enteredFrame);
		}
		
		public function enteredFrame(e:Event):void {
			//trace("I dont think if this function is getting executed");
			stage.addEventListener(KeyboardEvent.KEY_DOWN,moveBoy);
			removeEventListener(Event.ENTER_FRAME, enteredFrame);
		}
		
		
		
		public function moveBoy(e:KeyboardEvent):void 
		{
			
			FarmGame.boyIsMoving = true;
						
			
				if (e.keyCode == Keyboard.RIGHT) 
				{
					walkingSound.play();
					moveSprite("right",this.moveStepSize);
				}
				else if (e.keyCode == Keyboard.LEFT) 
				{
					walkingSound.play();
					moveSprite("left",this.moveStepSize);
				}
				else if (e.keyCode == Keyboard.UP) 
				{
					walkingSound.play();
					moveSprite("up",this.moveStepSize);
				}
				else if (e.keyCode == Keyboard.DOWN) 
				{
					walkingSound.play();
					moveSprite("down",this.moveStepSize);
				}
			
		}
		
		public function removeBoyKeyboardListener():void
		{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,moveBoy);
		}
		
		public function hitMiscreant(miscreantsArray:Array)
		{
			for (freeCounter_1=0; freeCounter_1<miscreantsArray.length; freeCounter_1 = freeCounter_1 + 1)
			{
				if (this.hitTestObject(miscreantsArray[freeCounter_1]))
				{
					if (miscreantsArray[freeCounter_1].caughtTheFruit == false)
					{
						miscreantsArray[freeCounter_1].y = miscreantsArray[freeCounter_1].y + miscreantsArray[freeCounter_1].moveStepSize;
						miscreantsArray[freeCounter_1].movingUp = false;
						if (miscreantsArray[freeCounter_1].y>(constraint_down - 50))
						{
							miscreantsArray[freeCounter_1].escaped = true;
							
						}
					}
					else
					{
						caughtTheThief = true;
						thiefIndex = freeCounter_1;
						stage.removeEventListener(KeyboardEvent.KEY_DOWN,moveBoy);
						
						//p1.startTheDialogues(miscreantsArray[freeCounter_1].x, miscreantsArray[freeCounter_1].y);
					}
				}
			}
		}
		
		/*public function gotoNextFrame(e:Event):void{
			myTween.removeEventListener(TweenEvent.MOTION_FINISH, gotoNextFrame);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,moveBoy);
			Market.goNextFrame = true;
		}*/
		
		
	}
}